package com.shujia.custom.udf;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaStringObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

public class MyUDFDemo3 extends GenericUDF {
    private String outString;

    /**
     * initialize方法主要是进行初始化，指定输出的数据类型
     * 需求：将输入进来的字符串变成 数加: s
     */
    @Override
    public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
        outString = "";
        return PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    }

    /**
     * evaluate方法主要是计算逻辑
     */
    @Override
    public Object evaluate(DeferredObject[] arguments) throws HiveException {
        if(arguments.length>=1){
            outString = "数加: " + arguments[0].get().toString();
        }

        return outString;
    }

    @Override
    public String getDisplayString(String[] children) {
        return "这是我们自己自定义的UDF函数";
    }
}
